package blog.utils;

import blog.dao.pojo.SysUser;
import blog.utils.JwtUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
public class CheckToken {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    public SysUser checkToken(String token) throws JsonProcessingException {

        if (token==null) return null;

        JwtUtils.checkToken(token); // 检验token合法性

        String redisUserInfo = redisTemplate.opsForValue().get("TOKEN_" + token);

        if (redisUserInfo==null) return null; // redis里面没有用户信息，没有该用户

        SysUser sysUser = new ObjectMapper().readValue(redisUserInfo, SysUser.class);

        return sysUser;
    }

}
